package top.huxy.boot.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.util.Random;
import java.util.StringJoiner;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

public class StringUtils {

    public static String encryption(String plainText) {
        if (plainText == null) throw new NullPointerException("不可以对NULL对象进行加密");
        String s = "lDAztZCNi4TPXBQs96k1E2nVUKYGFWvhJHImRowSbfayc038q7pdeOguLx5jMr";
        int hash = plainText.hashCode();
        StringBuilder res = new StringBuilder();
        Random r = new Random(hash);
        for (int i = 0; i < 32; i++) {
            int key = r.nextInt(62);
            res.append(s.charAt(key));
        }
        return res.toString();
    }

    public static boolean isEmpty(Object... args) {
        if (args.length == 0) return true;
        for (Object item : args) {
            if (item == null || item.toString().trim().length() == 0) return true;
        }
        return false;
    }

    public static String getEmptyParamName(String names, Object... args) {
        if (args.length == 0) {
            return null;
        }
        String[] nameArr = names.split(",");
        if (nameArr.length != args.length) {
            throw new IllegalArgumentException("names字符串中元素需要和args可变参数个数相同");
        }
        for (int i = 0; i < args.length; i++) {
            Object item = args[i];
            if (item == null || item.toString().length() == 0) {
                return nameArr[i].trim();
            }
        }
        return null;
    }

    /**
     * 获取一个唯一的值
     *
     * @return
     */
    public static String unique() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static String substringBefore(String str, String separator) {
        if (!isEmpty(str) && separator != null) {
            if (separator.isEmpty()) {
                return "";
            } else {
                int pos = str.indexOf(separator);
                return pos == -1 ? str : str.substring(0, pos);
            }
        } else {
            return str;
        }
    }

    /**
     * @param str name,age,state
     * @return #{name},#{age},#{state}
     */
    public static String name2param(String str) {
        String[] split = str.split(",");
        StringBuilder sb = new StringBuilder();
        for (String s : split) {
            sb.append("#{" + s + "},");
        }
        return sb.toString();
    }

    public static String getRandomNumberString(int length) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(ThreadLocalRandom.current().nextInt(10));
        }
        return sb.toString();
    }

    /**
     * 获取堆栈信息
     *
     * @param throwable 异常
     * @return
     */
    public static String getStackTrace(Throwable throwable) {
        if (throwable == null) return "throwable is null;";

        try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)) {
            throwable.printStackTrace(pw);
            return sw.toString();
        } catch (IOException e) {
            return "getStackTrace error";
        }
    }

    public static String getSimpleClassName(Class<?>[] arr) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < arr.length; i++) {
            if (i != arr.length - 1) {
                sb.append(arr[i].getSimpleName()).append(",");
            } else {
                sb.append(arr[i].getSimpleName());
            }
        }
        return sb.toString();
    }

    public static String merge(Object... args) {
        if (args.length == 0) return "";
        StringBuilder sb = new StringBuilder();
        for (Object arg : args) {
            sb.append(arg);
        }
        return sb.toString();
    }

    /**
     * 源自Spring的MethodHandler中的initDescription方法
     */
    public static String getMethodDescription(Method method) {
        if (method == null) {
            return null;
        }
        StringJoiner joiner = new StringJoiner(", ", "(", ")");
        for (Class<?> paramType : method.getParameterTypes()) {
            joiner.add(paramType.getSimpleName());
        }
        return method.getDeclaringClass().getName() + "#" + method.getName() + joiner.toString();
    }

    public static Object getArray0(Object obj) {
        if (obj == null) return null;
        if (obj.getClass().isArray()) {
            Object[] arr = (Object[]) obj;
            if (arr.length > 0) {
                return arr[0];
            } else {
                return null;
            }
        }
        return obj;
    }

    public static String stringMax32(String str) {
        if (str == null) return null;
        if (str.length() > 32) return str.substring(0, 32);
        return str;
    }

    public static String or(Object obj, String defaultValue) {
        if (obj == null) return defaultValue;
        if (StringUtils.isEmpty(obj)) return defaultValue;
        return obj.toString();
    }

    public static boolean isNaN(Object id) {
        if (isEmpty(id)) return true;
        try {
            Integer.parseInt(id.toString());
        } catch (Exception e) {
            return true;
        }
        return false;
    }
}
